// personalTaxData.js
const personalTaxData = {
    "320722198311021616": {
        idNumber: "320722198311021616",
        taxpayerId: "320722198311021616",
        name: "王跃华",
        idType: "居民身份证",
        registrationType: "私营有限责任公司",
        industry: "其他房屋建筑业",
        industryCategory: "建筑业",
        taxRecords: [
            {
                taxCategory: "个人所得税",
                taxItem: "综合所得",
                taxAmountDue: 777.32,
                taxStatus: "已入库",
                filingDate: "2024-06-01",
                depositDate: "2024-06-04",
                taxPeriodStart: "2023-01-01",
                taxPeriodEnd: "2023-12-31",
                taxNature: "一般申报",
                paymentMethod: "微信(app)",
                budgetItem: "其他个人所得税",
                budgetAllocationRatio: "中央60%、区(县)级40%",
                receivingTreasury: "国家金库连云港市海州区支库",
                voucherType: "税收电子缴款书",
                electronicVoucherNumber: "624060111140267895",
                collectingAuthority: "国家税务总局连云港市税务局第三税务分局一股",
                managingTaxOffice: "连云港市海州区",
                streetTown: "新海街道"
            }
        ],
        creditScore: 88,
        totalTaxPaid: 777.32,
        complianceStatus: "良好"
    }
};

/**
 * 根据身份证号、纳税号、姓名查询个人税务数据
 * @param {Object} query - 查询条件
 * @param {string} [query.idNumber] - 身份证号
 * @param {string} [query.taxpayerId] - 纳税人识别号
 * @param {string} [query.name] - 姓名
 * @returns {Object|null} - 查询结果
 */
export function getPersonalTaxData(query = {}) {
    const { idNumber, taxpayerId, name } = query;

    // 优先按身份证号精确匹配
    if (idNumber && personalTaxData[idNumber]) {
        return personalTaxData[idNumber];
    }

    // 按纳税号精确匹配
    if (taxpayerId) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].taxpayerId === taxpayerId) {
                return personalTaxData[key];
            }
        }
    }

    // 按姓名匹配
    if (name) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].name === name) {
                return personalTaxData[key];
            }
        }
    }

    // 联合查询：姓名+身份证号
    if (name && idNumber) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].name === name &&
                personalTaxData[key].idNumber === idNumber) {
                return personalTaxData[key];
            }
        }
    }

    // 联合查询：姓名+纳税号
    if (name && taxpayerId) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].name === name &&
                personalTaxData[key].taxpayerId === taxpayerId) {
                return personalTaxData[key];
            }
        }
    }

    return null;
}

// 支持多种查询方式的函数
export function searchPersonalTaxData(idNumber, taxpayerId, name) {
    // 单一查询
    if (idNumber && !taxpayerId && !name) {
        return personalTaxData[idNumber] || null;
    }

    if (taxpayerId && !idNumber && !name) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].taxpayerId === taxpayerId) {
                return personalTaxData[key];
            }
        }
        return null;
    }

    if (name && !idNumber && !taxpayerId) {
        for (const key in personalTaxData) {
            if (personalTaxData[key].name === name) {
                return personalTaxData[key];
            }
        }
        return null;
    }

    // 联合查询
    if (idNumber || taxpayerId || name) {
        const query = {};
        if (idNumber) query.idNumber = idNumber;
        if (taxpayerId) query.taxpayerId = taxpayerId;
        if (name) query.name = name;
        return getPersonalTaxData(query);
    }

    return null;
}

export default personalTaxData;
